package org.zachary.commonservice.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.zachary.commonservice.entity.ServiceType;
import org.zachary.commonservice.strategy.CreditStrategy;
import org.zachary.commonservice.strategy.factory.StrategyRegistry;

import java.util.Map;

@Slf4j
@Configuration
public class StrategyConfig {
    // 自动注入所有 CreditStrategy 实现类的 Bean
    @Autowired
    private Map<String, CreditStrategy> strategyMap; // key: Bean名称, value: 策略实例

    @Bean
    public StrategyRegistry strategyRegistry() {
        StrategyRegistry registry = new StrategyRegistry();
        strategyMap.forEach((beanName, strategy) -> {
            ServiceType serviceType = strategy.getServiceType();
            registry.register(serviceType, strategy);
            log.info("策略注册: {} -> {}", serviceType, strategy.getClass().getSimpleName());
        });
        return registry;
    }
}
